/*********************************************Copyright (c)***********************************************
** 
**
**--------------File Info---------------------------------------------------------------------------------
** File name:               none
** Latest modified Date:    none
** Latest Version:          none
** Descriptions:            none
**
**--------------------------------------------------------------------------------------------------------
** Created by:              Huangshihong
** Created date:            2021-05-26
** Version:                 V1.00
** Descriptions:            The original version
**
**--------------------------------------------------------------------------------------------------------
** Modified by:             
** Modified date:           
** Version:                 
** Descriptions:            
**
*********************************************************************************************************/

/********************************************************************************************************
** Includes
*********************************************************************************************************/
#include "io_output.h"
#include "pca9535.h"
#include "log.h"

/********************************************************************************************************
** Defined
*********************************************************************************************************/
#define PUMP_PORT_ARRAY_SIZE                    20

/********************************************************************************************************
** Global variable
*********************************************************************************************************/
typedef struct {
    GPIO_TypeDef* GPIOx;
    u16 GPIO_Pin;
}pump_port_t;

static pump_port_t pump_port_array[PUMP_PORT_ARRAY_SIZE] = {
{GPIOA,GPIO_PIN_4},
{GPIOA,GPIO_PIN_5},
{GPIOA,GPIO_PIN_6},
{GPIOA,GPIO_PIN_7},
{GPIOC,GPIO_PIN_4},
{GPIOC,GPIO_PIN_5},
{GPIOB,GPIO_PIN_0},
{GPIOB,GPIO_PIN_1},
{GPIOF,GPIO_PIN_11},
{GPIOF,GPIO_PIN_12},
{GPIOF,GPIO_PIN_13},
{GPIOF,GPIO_PIN_14},
{GPIOF,GPIO_PIN_15},
{GPIOG,GPIO_PIN_0},
{GPIOG,GPIO_PIN_1},
{GPIOE,GPIO_PIN_7},
{GPIOE,GPIO_PIN_8},
{GPIOE,GPIO_PIN_9},
{GPIOE,GPIO_PIN_10},
{GPIOE,GPIO_PIN_11}
};


/*********************************************************************************************************
** Function name:      none
** Descriptions:       none
** input parameters:   none
** output parameters:  none
** Returned value:     none
** Created by:         Huangshihong
** Created Date:       2021-05-28
**--------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
*********************************************************************************************************/
void device_light_on_off(u8 led,u8 state)
{
    if (led == DEVICE_LIGHT_RED) {
        if (state == STATUS_LIGHT_ON) {
            pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,4,STATUS_LIGHT_ON);
        } else {
            pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,4,STATUS_LIGHT_OFF);
        }
    } else if (led == DEVICE_LIGHT_GREEN) {
        if (state == STATUS_LIGHT_ON) {
            pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,5,STATUS_LIGHT_ON);
        } else {
            pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,5,STATUS_LIGHT_OFF);
        } 
    } else if (led == DEVICE_LIGHT_YELLOW) {
        if (state == STATUS_LIGHT_ON) {
            pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,4,STATUS_LIGHT_ON);
            pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,5,STATUS_LIGHT_ON);
        } else {
            pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,4,STATUS_LIGHT_OFF);
            pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,5,STATUS_LIGHT_OFF);
        } 
    } 
}

void slide_shelf_light_on_off(u8 num,u8 led,u8 state)
{
    if (num == 1) {
        if (led == SLIDE_SHELF_LIGHT_RED) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,0,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,0,STATUS_LIGHT_OFF);
            }
        } else if (led == SLIDE_SHELF_LIGHT_GREEN) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,1,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,1,STATUS_LIGHT_OFF);
            } 
        } else if (led == SLIDE_SHELF_LIGHT_BLUE) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,2,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,2,STATUS_LIGHT_OFF);
            } 
        } else if (led == SLIDE_SHELF_LIGHT_PURPLE) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,0,STATUS_LIGHT_ON);
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,2,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,0,STATUS_LIGHT_OFF);
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,2,STATUS_LIGHT_OFF);
            } 
        } 
    } else if (num == 2) {
        if (led == SLIDE_SHELF_LIGHT_RED) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,3,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,3,STATUS_LIGHT_OFF);
            }
        } else if (led == SLIDE_SHELF_LIGHT_GREEN) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,4,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,4,STATUS_LIGHT_OFF);
            } 
        } else if (led == SLIDE_SHELF_LIGHT_BLUE) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,5,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,5,STATUS_LIGHT_OFF);
            } 
        } else if (led == SLIDE_SHELF_LIGHT_PURPLE) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,3,STATUS_LIGHT_ON);
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,5,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,3,STATUS_LIGHT_OFF);
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,5,STATUS_LIGHT_OFF);
            } 
        } 
    } else if (num == 3) {
        if (led == SLIDE_SHELF_LIGHT_RED) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,6,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,6,STATUS_LIGHT_OFF);
            }
        } else if (led == SLIDE_SHELF_LIGHT_GREEN) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,7,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,7,STATUS_LIGHT_OFF);
            } 
        } else if (led == SLIDE_SHELF_LIGHT_BLUE) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,0,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,0,STATUS_LIGHT_OFF);
            } 
        } else if (led == SLIDE_SHELF_LIGHT_PURPLE) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,6,STATUS_LIGHT_ON);
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,0,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,6,STATUS_LIGHT_OFF);
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,0,STATUS_LIGHT_OFF);
            } 
        } 
    } else if (num == 4) {
    if (led == SLIDE_SHELF_LIGHT_RED) {
        if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,1,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,1,STATUS_LIGHT_OFF);
            }
        } else if (led == SLIDE_SHELF_LIGHT_GREEN) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,2,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,2,STATUS_LIGHT_OFF);
            } 
        } else if (led == SLIDE_SHELF_LIGHT_BLUE) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,3,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,3,STATUS_LIGHT_OFF);
            } 
        } else if (led == SLIDE_SHELF_LIGHT_PURPLE) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,1,STATUS_LIGHT_ON);
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,3,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,1,STATUS_LIGHT_OFF);
                pca9535_write_bit(&iic1_hander,PCA9535_C_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,3,STATUS_LIGHT_OFF);
            } 
        } 
    } 
}

void reagent_shelf_light_on_off(u8 num,u8 led,u8 state)
{
    if (num == 1) {
        if (led == REAGENT_SHELF_LIGHT_RED) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,0,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,0,STATUS_LIGHT_OFF);
            }
        } else if (led == REAGENT_SHELF_LIGHT_GREEN) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,1,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,1,STATUS_LIGHT_OFF);
            } 
        } 
    } else if (num == 2) {
        if (led == REAGENT_SHELF_LIGHT_RED) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,2,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,2,STATUS_LIGHT_OFF);
            }
        } else if (led == REAGENT_SHELF_LIGHT_GREEN) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,3,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,3,STATUS_LIGHT_OFF);
            } 
        } 
    } else if (num == 3) {
    if (led == REAGENT_SHELF_LIGHT_RED) {
        if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,4,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,4,STATUS_LIGHT_OFF);
            }
        } else if (led == REAGENT_SHELF_LIGHT_GREEN) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,5,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,5,STATUS_LIGHT_OFF);
            } 
        } 
    } else if (num == 4) {
        if (led == REAGENT_SHELF_LIGHT_RED) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,6,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,6,STATUS_LIGHT_OFF);
            }
        } else if (led == REAGENT_SHELF_LIGHT_GREEN) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,7,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT0_REG,7,STATUS_LIGHT_OFF);
            } 
        } 
    } else if (num == 5) {
        if (led == REAGENT_SHELF_LIGHT_RED) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,0,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,0,STATUS_LIGHT_OFF);
            }
        } else if (led == REAGENT_SHELF_LIGHT_GREEN) {
            if (state == STATUS_LIGHT_ON) {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,1,STATUS_LIGHT_ON);
            } else {
                pca9535_write_bit(&iic1_hander,PCA9535_B_DEV_ADDR,TCA9535_OUTPUT_PORT1_REG,1,STATUS_LIGHT_OFF);
            } 
        } 
    }
}

static void pump_init(void)
{
    u8 i = 0;
    GPIO_InitTypeDef GPIO_Initure;
    __HAL_RCC_GPIOA_CLK_ENABLE();  
    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_GPIOC_CLK_ENABLE();
    __HAL_RCC_GPIOE_CLK_ENABLE();
    __HAL_RCC_GPIOF_CLK_ENABLE();
    __HAL_RCC_GPIOG_CLK_ENABLE();

    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;  
    GPIO_Initure.Pull=GPIO_PULLUP;          
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;  

    for (i = 0;i < PUMP_PORT_ARRAY_SIZE;i++) {
       GPIO_Initure.Pin =  pump_port_array[i].GPIO_Pin;
       HAL_GPIO_Init(pump_port_array[i].GPIOx,&GPIO_Initure);
    }
}

void pump_on_off(u8 pump_num,u8 state)
{
    if (state == STATUS_PUMP_ON) {
        HAL_GPIO_WritePin(pump_port_array[pump_num].GPIOx,pump_port_array[pump_num].GPIO_Pin,GPIO_PIN_RESET);
    } else {
        HAL_GPIO_WritePin(pump_port_array[pump_num].GPIOx,pump_port_array[pump_num].GPIO_Pin,GPIO_PIN_SET);
    }
}

static void integral_fan_init(void)
{
    GPIO_InitTypeDef GPIO_Initure;
    __HAL_RCC_GPIOC_CLK_ENABLE();  
    
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;  
    GPIO_Initure.Pull=GPIO_PULLUP;          
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;

    GPIO_Initure.Pin =  INTEGRAL_FAN_PIN_1|INTEGRAL_FAN_PIN_2|INTEGRAL_FAN_PIN_3|INTEGRAL_FAN_PIN_4;
    HAL_GPIO_Init(INTEGRAL_FAN_PORT_1,&GPIO_Initure);

}

void integral_fan(u8 state)
{
    if (state == STATUS_FAN_ON) {
        HAL_GPIO_WritePin(INTEGRAL_FAN_PORT_1,INTEGRAL_FAN_PIN_1,GPIO_PIN_RESET);
        HAL_GPIO_WritePin(INTEGRAL_FAN_PORT_2,INTEGRAL_FAN_PIN_2,GPIO_PIN_RESET);
        HAL_GPIO_WritePin(INTEGRAL_FAN_PORT_3,INTEGRAL_FAN_PIN_3,GPIO_PIN_RESET);
        HAL_GPIO_WritePin(INTEGRAL_FAN_PORT_4,INTEGRAL_FAN_PIN_4,GPIO_PIN_RESET);
    } else {
        HAL_GPIO_WritePin(INTEGRAL_FAN_PORT_1,INTEGRAL_FAN_PIN_1,GPIO_PIN_SET);
        HAL_GPIO_WritePin(INTEGRAL_FAN_PORT_2,INTEGRAL_FAN_PIN_2,GPIO_PIN_SET);
        HAL_GPIO_WritePin(INTEGRAL_FAN_PORT_3,INTEGRAL_FAN_PIN_3,GPIO_PIN_SET);
        HAL_GPIO_WritePin(INTEGRAL_FAN_PORT_4,INTEGRAL_FAN_PIN_4,GPIO_PIN_SET);
    }
}

static void buzzer_init(void)
{
    GPIO_InitTypeDef GPIO_Initure;
    __HAL_RCC_GPIOI_CLK_ENABLE();  
    
    GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP;  
    GPIO_Initure.Pull=GPIO_PULLUP;          
    GPIO_Initure.Speed=GPIO_SPEED_HIGH;

    GPIO_Initure.Pin =  BUZZER_FAN_PIN;
    HAL_GPIO_Init(BUZZER_PORT,&GPIO_Initure);

}

void buzzer_on_off(u8 state)
{
    if (state == STATUS_BUZZER_ON) {
        HAL_GPIO_WritePin(BUZZER_PORT,BUZZER_FAN_PIN,GPIO_PIN_RESET);
    } else {
        HAL_GPIO_WritePin(BUZZER_PORT,BUZZER_FAN_PIN,GPIO_PIN_SET);
    }
}

void io_output_init(void)
{
    pump_init();
    integral_fan_init();
    buzzer_init();
}



